package com.xinsoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.*;
import com.xinsoft.entity.po.SysConfig;
import com.xinsoft.entity.po.SysMaterial;
import com.xinsoft.service.SyncPushBillService;
import com.xinsoft.service.SysConfigService;
import com.xinsoft.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class SyncPushBillAdapter {

    private SyncPushBillService syncPushBillService;

    @Autowired
    private SyncPushBillKisImpl syncPushBillKisImpl;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SysConfigService configService;

    private String ERP_CONNECT_TYPE;

    @PostConstruct
    private void init() {
        //读取配置文件，然后给 syncTemplateService 赋初始值
        List<SysConfig> sysConfigDOS = configService.list(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamKey, Const.ERP_CONNECT_TYPE).or().eq(SysConfig::getParamKey, Const.ERP_CONNECT_TYPE));
        sysConfigDOS.forEach(item ->{
            if ("ERP_CONNECT_TYPE".equals(item.getParamKey()))
                ERP_CONNECT_TYPE = item.getParamValue();
        });
    }

    /**
     * 生产领料
     * @return
     */
    public Response<?> feedingOutStock(List<ErpInOrOutStockParam> params) {
        Boolean aBoolean = this.limitFrequentCalls("生产领料", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.feedingOutStock(params);
        return Response.succeed(msg);
    }

    /**
     * 生产退料
     * @return
     */
    public Response<?> feedingInStock(List<ErpInOrOutStockParam> params) {
        Boolean aBoolean = this.limitFrequentCalls("生产退料", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.feedingInStock(params);
        return Response.succeed(msg);
    }

    /**
     * 其他入库
     * @return
     */
    public Response<?> otherInStock(List<ErpInOrOutStockParam> params) {
        Boolean aBoolean = this.limitFrequentCalls("其他入库", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.otherInStock(params);
        return Response.succeed(msg);
    }

    /**
     * 其他出库
     * @return
     */
    public Response<?> otherOutStock(List<ErpInOrOutStockParam> params) {
        Boolean aBoolean = this.limitFrequentCalls("其他出库", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.otherOutStock(params);
        return Response.succeed(msg);
    }

    /**
     * 委外出库
     * @return
     */
    public Response<?> sourcingFeedingOutStock(List<ErpInOrOutStockParam> params) {
        Boolean aBoolean = this.limitFrequentCalls("委外出库", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.sourcingFeedingOutStock(params);
        return Response.succeed(msg);
    }

    /**
     * 委外退料
     * @return
     */
    public Response<?> sourcingReturnInStock(List<ErpInOrOutStockParam> params) {
        Boolean aBoolean = this.limitFrequentCalls("委外退料", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.sourcingReturnInStock(params);
        return Response.succeed(msg);
    }

    /**
     * 委外入库
     * @param params 出入库参数
     * @return {@link Response}<{@link ?}>
     */
    public Response<?> sourcingFeedingInStock(List<ErpInOrOutStockParam> params) {
        Boolean aBoolean = this.limitFrequentCalls("委外入库", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.sourcingFeedingInStock(params);
        return Response.succeed(msg);
    }

    /**
     * 生产入库
     * @param params 出入库参数
     * @return {@link Response}<{@link ?}>
     */
    public Response<?> produceInStock(List<ErpInOrOutStockParam> params) {
        Boolean aBoolean = this.limitFrequentCalls("生产入库", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.produceInStock(params);
        return Response.succeed(msg);
    }

    /**
     * 采购入库
     * @param params 出入库参数
     * @return {@link Response}<{@link ?}>
     */
    public Response<?> purchaseInStock(List<ErpInOrOutStockParam> params) {
        Boolean aBoolean = this.limitFrequentCalls("采购入库", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.purchaseInStock(params);
        return Response.succeed(msg);
    }

    /**
     * 采购退货
     * @param params 出入库参数
     * @return {@link Response}<{@link ?}>
     */
    public Response<?> purchaseOutStock(List<ErpInOrOutStockParam> params) {
        Boolean aBoolean = this.limitFrequentCalls("采购退货", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.purchaseOutStock(params);
        return Response.succeed(msg);
    }

    /**
     * 销售出库
     *
     * @param params 出入库参数
     * @return {@link Response}<{@link ?}>
     */
    public Response<?> saleOutStock(List<ErpInOrOutStockParam> params) {
        Boolean aBoolean = this.limitFrequentCalls("销售出库", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.saleOutStock(params);
        return Response.succeed(msg);
    }

    /**
     * 库存调拨
     *
     * @param params 库存调拨参数
     * @return {@link Response}<{@link ?}>
     */
    public Response<?> inventoryTransfer(List<ErpInOrOutStockParam> params) {
        Boolean aBoolean = this.limitFrequentCalls("库存调拨", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.inventoryTransfer(params);
        return Response.succeed(msg);
    }

    /**
     * 采购收料通知
     * @param params
     * @return {@link Response}<{@link ?}>
     */
    public KisApiResultVOS purchaseNotice(List<ErpNoticeBillParam> params) {
        KisApiResultVOS vos = null;
        if (ERP_CONNECT_TYPE.equals("1")) vos = syncPushBillKisImpl.purchaseNotice(params);
        return vos;
    }

    /**
     * 委外收料通知
     * @param params
     * @return {@link Response}<{@link ?}>
     */
    public KisApiResultVOS sourcingNotice(List<ErpNoticeBillParam> params) {
        KisApiResultVOS vos = null;
        if (ERP_CONNECT_TYPE.equals("1")) vos = syncPushBillKisImpl.sourcingNotice(params);
        return vos;
    }

    /**
     * 盘盈
     * @param profitList
     * @return {@link Response}<{@link ?}>
     */
    public Response<?> inventoryProfit(List<ErpInOrOutStockParam> profitList) {
        Boolean aBoolean = this.limitFrequentCalls("盘盈", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.inventoryProfit(profitList);
        return Response.succeed(msg);
    }


    /**
     * 盘亏
     * @param lossList
     * @return {@link Response}<{@link ?}>
     */
    public Response<?> inventoryLoss(List<ErpInOrOutStockParam> lossList) {
        Boolean aBoolean = this.limitFrequentCalls("盘亏", null);
        if (aBoolean) {
            return Response.fail("请勿频繁操作！");
        }
        String msg = null;
        if (ERP_CONNECT_TYPE.equals("1")) msg = syncPushBillKisImpl.inventoryLoss(lossList);
        return Response.succeed(msg);
    }



    /**限制频繁调用*/
    public Boolean limitFrequentCalls(String redisKey, Long sec){
        if (sec == null) {
            sec = 10L;
        }
        String key = TokenUtil.getUserId() + redisKey;
        Object value = redisTemplate.opsForValue().get(key);
        if (Objects.nonNull(value)) {
            Long expire = redisTemplate.getExpire(key, TimeUnit.SECONDS);
            return true;
        }
        //同一个登录人5秒内只可以操作一次
        redisTemplate.opsForValue().set(key, "Temporarily out of sync", sec, TimeUnit.SECONDS);
        return false;
    }
}
